import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:get/get.dart';
import 'package:logmanager/pages/login/login_controller.dart';
import 'package:logmanager/utils/string_ext.dart';

import '../../ui/theme/xlcolors.dart';
import '../../ui/theme/xlsize.dart';
import '../../utils/input_formatters_util.dart';
import '../../widget/clear_text_field.dart';

class LoginPage extends GetView<LoginController> {
  var captchaClickable = false;
  TextEditingController? _phoneController;
  TextEditingController? _codeController;

  @override
  Widget build(BuildContext context) {
    _phoneController = TextEditingController();
    _phoneController?.text = "17600000003";
    _codeController = TextEditingController();
    _codeController?.text = "1228";
    // _codeController?.text = "122888";

    // controller.setLoginEnabled(phoneStr:_phoneController?.text,codeStr: _codeController?.text,passwordStr:"");
    return Column(
      children: [
        SizedBox(height: 80),
        Image.asset(
          "assets/images/login_logo.png",
          width: 96,
          height: 80,
          fit: BoxFit.fill,
        ),

        Container(
          height: 44,
          margin: EdgeInsets.only(
              left: XlSize.loginInputMargin,
              top: 46,
              right: XlSize.loginInputMargin),
          child: ClearTextField(
            hintText: '请输入手机号',
            autofocus: false,
            bgColor: Color(0xFFF4F4F4),
            onTextChangeed: (e) {
              controller.btnCodeEnabled.value = e.length == 13;
              // controller.setLoginEnabled(
              //     phoneStr: e, codeStr: _codeController?.text);
            },
            controller: _phoneController,
            inputFormatters: InputFormattersUtil.phoneFormatters,
          ),
        ),

        Container(
          height: 44,
          margin: EdgeInsets.only(
              left: XlSize.loginInputMargin,
              top: 30,
              right: XlSize.loginInputMargin),
          child:  ClearTextField(
            hintText: '请输入验证码',
            controller: _codeController,
            onTextChangeed: (textCheanged) {
              controller.setLoginEnabled(
                  phoneStr: _phoneController?.text,
                  codeStr: textCheanged);
            },
            inputFormatters:
            InputFormattersUtil.codeFormatters,
            focusNode: controller.codeFocusNode,
            bgColor: XlColors.bgInputLogin,
          ),
        ),
        Container(
          width: double.infinity,
          height: 44,
          margin: const EdgeInsets.only(
              left: XlSize.loginInputMargin,
              top: 30,
              right: XlSize.loginInputMargin,
              bottom: 10),
          child: Obx(() => ElevatedButton(
            style: ButtonStyle(
                shape: MaterialStateProperty.all(
                    RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8))),
                backgroundColor: MaterialStateProperty.resolveWith(
                        (states) =>
                    states.contains(MaterialState.disabled)
                        ? XlColors.colorBtnDisabled
                        : XlColors.colorPrimary),
                foregroundColor:
                MaterialStateProperty.all(Colors.white)),
            onPressed: controller.btnLoginEnabled.value
                ? () {
              controller.login(
                  (_phoneController?.text ?? "")
                      .replaceAll(" ", ""),
                  _codeController?.text ?? null);
            }
                : null,
            child: const Text(
              "登录",
              style: TextStyle(
                  fontSize: 16, fontWeight: FontWeight.w400),
            ),
          )),
        ),
      ],
    );
  }
}
